package com.unit6.util;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * @author 李琦 on 2021/8/17.
 * @version java version "16.0.1" 2021-04-20
 * 公共的连接工具类
 * jdbc:mysql://localhost:3306/data1?useSSL=false&serverTimezone=UTC
 * "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8"
 */
public class GetConnection {
    private static Connection conn=null;
    private static String url = "jdbc:mysql://localhost:3306/data1?useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8";
    //useUnicode=true&characterEncoding=UTF-8";

    private static String dataBaseName="root";
    private static String dataBasePwd="123456";
    public static Connection getConn(){
        if(conn==null){
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");//加载jdbc驱动
                conn= DriverManager.getConnection(url,dataBaseName,dataBasePwd);//连接数据库
                System.out.println("数据库已连接!");
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("数据库连接失败!");
            }
        }
        return conn;
    }
}
